using DeathReportManagement.Models;

namespace DeathReportManagement.Services;

public class HisIntegrationService
{
    private readonly IHttpClientFactory _httpClientFactory;
    private readonly IConfiguration _configuration;
    
    public HisIntegrationService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
    {
        _httpClientFactory = httpClientFactory;
        _configuration = configuration;
    }
    
    public async Task<PatientInfo?> GetPatientInfoAsync(string? inpatientNo, string? outpatientNo)
    {
        if (string.IsNullOrWhiteSpace(inpatientNo) && string.IsNullOrWhiteSpace(outpatientNo))
        {
            return null;
        }
        
        try
        {
            var httpClient = _httpClientFactory.CreateClient();
            var hisApiUrl = _configuration["HisSettings:ApiUrl"];
            
            if (string.IsNullOrWhiteSpace(hisApiUrl))
            {
                return await GetMockPatientInfoAsync(inpatientNo, outpatientNo);
            }
            
            var requestUrl = $"{hisApiUrl}/api/patient/info";
            var queryParams = new List<string>();
            
            if (!string.IsNullOrWhiteSpace(inpatientNo))
            {
                queryParams.Add($"inpatientNo={Uri.EscapeDataString(inpatientNo)}");
            }
            
            if (!string.IsNullOrWhiteSpace(outpatientNo))
            {
                queryParams.Add($"outpatientNo={Uri.EscapeDataString(outpatientNo)}");
            }
            
            if (queryParams.Count > 0)
            {
                requestUrl += "?" + string.Join("&", queryParams);
            }
            
            var response = await httpClient.GetAsync(requestUrl);
            
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                
                return await Task.FromResult<PatientInfo?>(null);
            }
            
            return null;
        }
        catch (Exception)
        {
            return await GetMockPatientInfoAsync(inpatientNo, outpatientNo);
        }
    }
    
    private async Task<PatientInfo?> GetMockPatientInfoAsync(string? inpatientNo, string? outpatientNo)
    {
        await Task.Delay(100);
        
        if (!string.IsNullOrWhiteSpace(inpatientNo) || !string.IsNullOrWhiteSpace(outpatientNo))
        {
            return new PatientInfo
            {
                Name = "张三",
                Gender = "男",
                BirthDate = new DateOnly(1980, 5, 15),
                IdNumber = "370102198005151234",
                Phone = "13800138000",
                Address = "山东省济南市历下区",
                Ethnicity = "汉族"
            };
        }
        
        return null;
    }
}

public class PatientInfo
{
    public string Name { get; set; } = string.Empty;
    public string Gender { get; set; } = string.Empty;
    public DateOnly BirthDate { get; set; }
    public string IdNumber { get; set; } = string.Empty;
    public string Phone { get; set; } = string.Empty;
    public string Address { get; set; } = string.Empty;
    public string Ethnicity { get; set; } = string.Empty;
}